// pages/pay/pay.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: 0,
    title: null,
    shop: null,
    shopUrl: null,
    num: 1,
    price: null,
    total: null,
    img: null,
    receiver: '',
    receiverPhone: '',
    receiverAddress: '',
    play:false,
    saveReceiver:null,
    savePhone:null,
    saveAddress:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (app.data.userInfo == null) {
      wx.redirectTo({
        url: '../login/login',
      })
    }else{
      this.setData({
        navH: app.globalData.navHeight,
        title: app.globalData.productInfor[app.globalData.productIndex].productName,
        shop: app.globalData.productInfor[app.globalData.productIndex].shop,
        shopUrl: app.globalData.productInfor[app.globalData.productIndex].shopUrl,
        img: app.globalData.productInfor[app.globalData.productIndex].cover
      })
      var that = this
      if (app.globalData.whichOne == 'groupPrice') {
        that.setData({
          price: app.globalData.productInfor[app.globalData.productIndex].productGroupPrice,
          total: app.globalData.productInfor[app.globalData.productIndex].productGroupPrice
        })
      } else {
        that.setData({
          price: app.globalData.productInfor[app.globalData.productIndex].productAlonePrice,
          total: app.globalData.productInfor[app.globalData.productIndex].productAlonePrice
        })
      }
      var that = this
      wx.request({
        url: 'http://localhost:8081/getuseraddress/' + app.data.userInfo[0].userId,

        success(res) {
          that.setData({
            receiver: res.data.realName,
            receiverPhone: res.data.userPhoneNumber,
            receiverAddress: res.data.userAddress
          })
        }
      })
    }
  },

  goBack: function() {
    wx.switchTab({
      url: '../index/index',
    })
  },

  bindMinus: function() {
    var num = this.data.num;
    if (num > 1) {
      num--;
    }
    // 将数值与状态写回
    this.setData({
      num: num,
      total: (num * this.data.price).toFixed(1)
    });
  },
  /* 点击加号 */
  bindPlus: function() {
    var num = this.data.num;
    // 不作过多考虑自增1
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态

    // 将数值与状态写回
    this.setData({
      num: num,
      total: (num * this.data.price).toFixed(1)
    });
  },
  /* 输入框事件 */
  bindManual: function(e) {
    var num = e.detail.value;
    // 将数值与状态写回
    this.setData({
      num: num,
      total: (num * this.data.price).toFixed(1)
    });
  },
  change:function(){
     this.setData({
       play:!this.data.play
     })
  },
  save: function () {
    if (this.data.savePhone == null) {
      wx.showToast({
        title: '请输入手机号',
        image: '../../img/login/error.png',
        duration: 2000
      })
      return false;
    }
    if (this.data.savePhone.length != 11) {
      wx.showToast({
        title: '手机号长度有误',
        image: '../../img/login/error.png',
        duration: 2000
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(this.data.savePhone)) {
      wx.showToast({
        title: '手机号无效',
        image: '../../img/login/error.png',
        duration: 2000
      })
      return false;
    }
    this.setData({
      play: !this.data.play,
      receiver:this.data.saveReceiver,
      receiverPhone:this.data.savePhone,
      receiverAddress:this.data.saveAddress
    })
    wx.request({
      url: 'http://localhost:8081/updateuserinfor/'+this.data.receiverPhone+'/'+this.data.receiverAddress+'/'+this.data.receiver+'/'+app.data.userInfo[0].userId,
    })
  },
  getReceiver: function (e) {
    this.setData({
      saveReceiver: e.detail.value
    })
  },
  getPhone: function (e) {
    this.setData({
      savePhone: e.detail.value
    })
  },
  getAddress: function (e) {
    this.setData({
      saveAddress: e.detail.value
    })
  },
  purchasing:function(){
    wx.request({
      url: 'http://localhost:8081/addorder/' + app.data.userInfo[0].userId + '/' + this.data.title + '/' + this.data.price + '/' + this.data.num + '/' + this.data.total,
    })
    wx.showToast({
      title: '支付成功',
      icon: 'success',
      duration: 2000
    })
  }
})